package com.ruoyi.electricity.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.electricity.domain.ElectricityData;
import com.ruoyi.electricity.domain.ElectricityMeterData;
import com.ruoyi.electricity.domain.UndergroundElectricitySystemDemoJsonStr;
import com.ruoyi.electricity.mapper.ElectricityDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class UndergroundElectricitySystem {
    @Value("${UNDERGROUND_ELECTRICITY_SYSTEM_SWITCH}")
    private boolean electricitySystemSwitch;
    @Value("${UNDERGROUND_ELECTRICITY_SYSTEM_IP}")
    private String electricitySystemIp;
    private final RestTemplate restTemplate;
    private final ObjectMapper objectMapper = new ObjectMapper();
    private final ElectricityDataMapper electricityDataMapper;
    // 构造器注入RestTemplate，@Autowired可省略
    public UndergroundElectricitySystem(RestTemplate restTemplate,ElectricityDataMapper electricityDataMapper) {
        this.restTemplate = restTemplate;
        this.electricityDataMapper = electricityDataMapper;
    }



    /**
     * 查询用电设备的实时数据
     */
    public String getRealTimeElectricityData() {
        if (electricitySystemSwitch == false) {
            return UndergroundElectricitySystemDemoJsonStr.electricityData;
        }
        String url = "http://"+ electricitySystemIp + ":48001/output/meterenergy";
        System.out.println(url);
        String res = restTemplate.getForObject(url, String.class);
        return res;
    }


    /**
     * 查询用电设备的实时数据
     */
    public List<ElectricityMeterData> fetchMeterEnergyData() throws JsonProcessingException {
        List<ElectricityMeterData> meterDataList = new ArrayList<>();

        if (electricitySystemSwitch) {
            String url = "http://"+ electricitySystemIp + ":48001/output/meterenergy";
//            String url = "http://127.0.0.1:8080/electricity/electricityData/output/meterenergy";
            String responseJson = restTemplate.getForObject(url, String.class);

            // 将JSON字符串转换为ElectricityMeterData列表
            meterDataList = objectMapper.readValue(responseJson,
            objectMapper.getTypeFactory().constructCollectionType(List.class, ElectricityMeterData.class));
        }

        // 从数据库中获取当天的用电数据
        List<ElectricityMeterData> electricityDataList = electricityDataMapper.selectElectricityDataTodayList();
        for(ElectricityMeterData data:electricityDataList)
        {
            System.out.println(data.getInsertTime());
        }
        // 将 electricityDataList 转换为一个 HashMap，以 EquipCode 为键
        Map<Integer, Double> electricityDataMap = new HashMap<>();
        for (ElectricityMeterData data : electricityDataList) {
            electricityDataMap.put(data.getMeterID(), data.getpAEnergy());
        }
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        // 处理 meterDataList
        for (ElectricityMeterData meterData : meterDataList) {
            Integer meterId = meterData.getMeterID();
            Double paEnergyDataNow = meterData.getpAEnergy();

            if (electricityDataMap.containsKey(meterId)) {
                Double paEnergyDataMIN = electricityDataMap.get(meterId);
                meterData.setpAEnergyToday( paEnergyDataNow-paEnergyDataMIN);
            }
        }

        return meterDataList;
    }

}
